package electricity.controller;

import electricity.Utils.AliOssUtil;
import electricity.domain.EquipmentRepair;
import electricity.dto.DeviceQueryDTO;
import electricity.dto.RepairDto;
import electricity.result.Result;
import electricity.service.RepairService;
import electricity.vo.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

@RestController
@RequestMapping("/repair")
@Tag(name = "报修管理")
@Slf4j
public class RepairController {

    @Autowired
    private RepairService repairService;
    @Autowired
    private AliOssUtil aliyunOssUtil;

    @GetMapping("/list")
    @Operation(summary = "报修列表")
    public Result<PageResult>page(RepairDto repairDto) {
        log.info("设备分页查询:{}", repairDto);


        PageResult pageResult = repairService.pageQuery(repairDto);
        return Result.success(pageResult);

    }


    @PutMapping("/updateProcessor")
    @Operation(summary = "修改处理人")
    public Result updateProcessor(@RequestParam Integer repairId, @RequestParam Integer processorId) {
        log.info("修改处理人:{},{}", repairId, processorId);
        repairService.updateProcessor(repairId, processorId);
        return Result.success();
    }

    @PutMapping("/update")
    @Operation(summary = "修改报修单")
    public Result update(@RequestBody EquipmentRepair equipmentRepair) {
        log.info("修改报修单:{}", equipmentRepair);
        equipmentRepair.setRepairTime(new Date());
        repairService.update(equipmentRepair);
        return Result.success();
    }

    @PostMapping("/upload")
    @Operation(summary = "上传报修单图片")
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
        log.info("上传图片:{}", file.getOriginalFilename());

        String bucketName = "shangchuantupianqq"; // 指定 bucketName
        String directory = "repairs"; // 你可以根据需要修改目录
        String fileName = file.getOriginalFilename();

        String url = aliyunOssUtil.uploadFile(file.getInputStream(), bucketName, directory, fileName);
        return Result.success(url);
    }

}
